package com.app.micaihu.custom.view.dialog;

import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.micaihu.R;
import com.app.micaihu.manager.UserInforManager;
import com.app.utils.util.ViewSizeUtils;
import com.baidu.mobstat.StatService;

/**
 * Created by Guo on 2016/7/21.
 * <p>
 * <p>
 * 自定义dialog,中间带有editText
 * 使用时输入activity,title,msg和editText的content还有点击事件的listener,重写里边的点击事件
 */
public class CustomDialog implements View.OnClickListener {

    private static CustomDialog customDialog;
    private OnClickedListener mOnClickedListener;
    private EditText etContent;
    private Activity activity;
    private String[] pers;
    private Dialog dialog;
    private onClickedThreeListener mOnClickedThreeListener;
    private Dialog dialogThreeClick;
    //private AlertDialog alertDialog;
    private Dialog dialogInvite;
    private OnClickInviteListener mOnClickInviteListener;
    private Dialog dialogLevel;
    private Dialog pushDialog;
    private Dialog permissionDialog;

    private CustomDialog() {
    }

    public static CustomDialog getInstance() {
        if (customDialog == null) {
            customDialog = new CustomDialog();
        }
        return customDialog;
    }


    @Override
    public void onClick(View v) {
        if (v == null) return;
        switch (v.getId()) {
            //修改昵称开始---------------------------------------------------------
            case R.id.iv_input_cancel://
                if (mOnClickedListener != null) {
                    mOnClickedListener.cancel();
                }
                break;
            case R.id.btn_negative:
                if (mOnClickedListener != null) {
                    mOnClickedListener.negative();
                    if (dialog != null && activity != null && !activity.isFinishing())
                        dialog.dismiss();
                }
                break;
            case R.id.btn_positive:
                if (mOnClickedListener != null) {
                    mOnClickedListener.positive();
                    if (dialog != null && !TextUtils.isEmpty(getContentStr()) && activity != null && !activity.isFinishing())
                        dialog.dismiss();
                }
                break;
            //修改昵称结束---------------------------------------------------------

            //修改头像开始---------------------------------------------------
            case R.id.tv_top:
                if (mOnClickedThreeListener != null) {
                    mOnClickedThreeListener.topBotton();
                    if (dialogThreeClick != null && activity != null && !activity.isFinishing())
                        dialogThreeClick.dismiss();
                }
                break;
            case R.id.tv_bottom:
                if (mOnClickedThreeListener != null) {
                    mOnClickedThreeListener.bottomBotton();
                    if (dialogThreeClick != null && activity != null && !activity.isFinishing())
                        dialogThreeClick.dismiss();
                }
                break;
            case R.id.tv_cancel:
                if (mOnClickedThreeListener != null) {
                    mOnClickedThreeListener.cancel();
                    if (dialogThreeClick != null && activity != null && !activity.isFinishing())
                        dialogThreeClick.dismiss();
                }
                break;
            //修改头像结束---------------------------------------------------

            //邀请好友开始---------------------------------------------------
            case R.id.tv_qq:
                if (mOnClickInviteListener != null) {

                    mOnClickInviteListener.qqInvite();
                }
                if (dialogInvite != null && activity != null && !activity.isFinishing()) {
                    dialogInvite.dismiss();
                    dialogInvite = null;
                }
                break;
            case R.id.tv_wx:
                if (mOnClickInviteListener != null) {

                    mOnClickInviteListener.wxInvite();
                }
                if (dialogInvite != null && activity != null && !activity.isFinishing()) {
                    dialogInvite.dismiss();
                    dialogInvite = null;
                }
                break;
            case R.id.tv_wxfriends:
                if (mOnClickInviteListener != null) {

                    mOnClickInviteListener.wxFriendsInvite();
                }
                if (dialogInvite != null && activity != null && !activity.isFinishing()) {
                    dialogInvite.dismiss();
                    dialogInvite = null;
                }
                break;

            //邀请好友结束---------------------------------------------------

            case R.id.rl_levelcontainer:
                if (dialogLevel != null && !this.activity.isFinishing()) dialogLevel.dismiss();
                break;


            //权限申请---------------------------------------------------

            case R.id.per_cancel:////取消弹窗
                if (permissionDialog != null && this.activity != null && !this.activity.isFinishing())
                    permissionDialog.dismiss();
                break;
            case R.id.per_next://请求权限,并不做回调
                if (permissionDialog != null && this.activity != null && !this.activity.isFinishing()) {
                    permissionDialog.dismiss();
                }
                if (pers != null && pers.length > 0) {

                }

                break;
            case R.id.per_setting://去设置页面
                if (permissionDialog != null && this.activity != null && !this.activity.isFinishing()) {
                    permissionDialog.dismiss();

                }
                if (activity != null && !this.activity.isFinishing()) {
                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
                    intent.setData(uri);
                    activity.startActivity(intent);
                }

                break;

        }
    }

    /**
     * 邀请好友的dialog
     *
     * @param activity
     * @param mOnClickInviteListener
     */
    public void showShareDialog(Activity activity, OnClickInviteListener mOnClickInviteListener) {
        this.activity = activity;
        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_inviteshare, null);
        LinearLayout llInviteContainer = (LinearLayout) view.findViewById(R.id.ll_invite_container);
        TextView tvQQ = (TextView) view.findViewById(R.id.tv_qq);
        TextView tvWX = (TextView) view.findViewById(R.id.tv_wx);
        TextView tvWXFriends = (TextView) view.findViewById(R.id.tv_wxfriends);

        if (tvQQ != null) tvQQ.setOnClickListener(this);
        if (tvWX != null) tvWX.setOnClickListener(this);
        if (tvWXFriends != null) tvWXFriends.setOnClickListener(this);

        if (llInviteContainer != null)
            llInviteContainer.getLayoutParams().width = ViewSizeUtils.getWidthPixels() / 5 * 4;

        setOnClickInviteListener(mOnClickInviteListener);

        if (!activity.isFinishing()) {
            dialogInvite = new Dialog(activity, R.style.CustomDialogStyle);
            dialogInvite.requestWindowFeature(Window.FEATURE_ACTION_MODE_OVERLAY);
            dialogInvite.setCanceledOnTouchOutside(true);
            dialogInvite.setContentView(view);
            dialogInvite.show();
        }
    }

    /*************************************
     * invite好友的回调
     ****************************/
    public interface OnClickInviteListener {
        void qqInvite();

        void wxInvite();

        void wxFriendsInvite();
    }


    public void setOnClickInviteListener(OnClickInviteListener mOnClickInviteListener) {
        this.mOnClickInviteListener = mOnClickInviteListener;
    }
    /************************************* invite好友的回调 ****************************/


    /**
     * 弹出带有edittext的dialog
     *
     * @param activity
     * @param title             dialog的title
     * @param msg               dialog的提示信息
     * @param content
     * @param onClickedListener 点击事件的监听
     */
    public void showConfirmDialog(Activity activity, String title, String msg, String content, OnClickedListener onClickedListener) {
        this.activity = activity;
        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_edit, null);
        TextView tvTitle = (TextView) view.findViewById(R.id.tv_title);
        TextView tvMsg = (TextView) view.findViewById(R.id.tv_msg);
        etContent = (EditText) view.findViewById(R.id.et_content);
        ImageView ivInputCancel = (ImageView) view.findViewById(R.id.iv_input_cancel);
        Button btnNegative = (Button) view.findViewById(R.id.btn_negative);
        Button btnPositive = (Button) view.findViewById(R.id.btn_positive);

        LinearLayout llDialogContainer = (LinearLayout) view.findViewById(R.id.ll_dialog_container);
        llDialogContainer.getLayoutParams().width = ViewSizeUtils.getWidthPixels() / 5 * 4;

        setOnClickedListener(onClickedListener);

        if (!activity.isFinishing()) {
            //
            dialog = new Dialog(activity, R.style.CustomDialogStyle);
            dialog.requestWindowFeature(Window.FEATURE_ACTION_MODE_OVERLAY);
            dialog.setCanceledOnTouchOutside(true);
            dialog.setContentView(view);
            dialog.show();

        }

        if (tvTitle != null) tvTitle.setText(title);
        if (tvMsg != null) tvMsg.setText(msg);
        if (etContent != null) etContent.setText(content);
        if (btnNegative != null) {
            btnNegative.setOnClickListener(this);

        }


        if (btnPositive != null) btnPositive.setOnClickListener(this);
        if (ivInputCancel != null) ivInputCancel.setOnClickListener(this);

    }


    /**
     * 获取editText中文本的方法
     *
     * @return
     */
    public String getContentStr() {
        String contentStr = "";
        if (etContent != null) {
            contentStr = etContent.getText().toString().trim();
        }
        return contentStr;
    }


    /**
     * 获取content控件对象
     *
     * @return
     */
    public EditText getEditTextView() {
        if (etContent != null) {
            return etContent;
        } else {
            return etContent = (EditText) LayoutInflater.from(activity).inflate(R.layout.dialog_edit, null).findViewById(R.id.et_content);
        }
    }


    /*****************************
     * 带edittext的dialog的 点击回调
     ****************************/
    public interface OnClickedListener {
        void cancel();

        void positive();

        void negative();
    }

    public void setOnClickedListener(OnClickedListener mOnClickedListener) {
        this.mOnClickedListener = mOnClickedListener;
    }
    /***************************** 带edittext的dialog的 点击回调 ****************************/


    /**
     * 从底部弹出的 dialog,暂时有上,下,和取消三个按钮
     */
    public void showThreeDialog(Activity activity, String top, String bottom, int dialogWindowAnim, onClickedThreeListener mOnClickedThreeListener) {
        this.activity = activity;

        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_head, null);
        TextView tvTop = (TextView) view.findViewById(R.id.tv_top);
        TextView tvBottom = (TextView) view.findViewById(R.id.tv_bottom);
        TextView tvCancel = (TextView) view.findViewById(R.id.tv_cancel);

        setOnClickedThreeListener(mOnClickedThreeListener);

        if (!activity.isFinishing()) {
            //
            dialogThreeClick = new Dialog(activity, R.style.CustomDialogStyle);
            dialogThreeClick.requestWindowFeature(Window.FEATURE_ACTION_MODE_OVERLAY);
            dialogThreeClick.getWindow().setGravity(Gravity.BOTTOM);
            dialogThreeClick.getWindow().setWindowAnimations(dialogWindowAnim);
            dialogThreeClick.setCanceledOnTouchOutside(true);
            dialogThreeClick.setContentView(view);
            dialogThreeClick.show();
        }

        if (tvTop != null) {
            tvTop.setOnClickListener(this);
            if (!TextUtils.isEmpty(top)) tvTop.setText(top);
        }
        if (tvBottom != null) {
            tvBottom.setOnClickListener(this);
            if (!TextUtils.isEmpty(bottom)) tvBottom.setText(bottom);
        }
        if (tvCancel != null) tvCancel.setOnClickListener(this);
    }


    /*****************************
     * 从底部弹出的,带三个按钮的 dialog 的回调
     ****************************/
    public interface onClickedThreeListener {
        void cancel();

        void topBotton();

        void bottomBotton();
    }

    public void setOnClickedThreeListener(onClickedThreeListener mOnClickedThreeListener) {

        this.mOnClickedThreeListener = mOnClickedThreeListener;
    }

    /***************************** 从底部弹出的,带三个按钮的 dialog 的回调****************************/


    /**
     * 升级的弹窗
     *
     * @param activity
     * @param previousRank
     * @param now
     */
    public void showLevelUpDialog(final Activity activity, String previousRank, String now) {
        this.activity = activity;
        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_levelup, null);
        RelativeLayout rlLevelContainer = (RelativeLayout) view.findViewById(R.id.rl_levelcontainer);
        TextView tvPrevious = (TextView) view.findViewById(R.id.tv_previous);
        TextView tvNow = (TextView) view.findViewById(R.id.tv_now);

        if (TextUtils.isEmpty(previousRank) || TextUtils.isEmpty(now)) {
            tvPrevious.setVisibility(View.GONE);
            tvNow.setVisibility(View.GONE);
        } else {
            tvPrevious.setText(previousRank);
            tvNow.setText(now);
        }
        rlLevelContainer.setOnClickListener(this);
        if (!activity.isFinishing()) {
            //
            dialogLevel = new Dialog(activity, R.style.CustomDialogStyle);
            dialogLevel.requestWindowFeature(Window.FEATURE_ACTION_MODE_OVERLAY);
            dialogLevel.getWindow().setGravity(Gravity.CENTER);
            dialogLevel.setCanceledOnTouchOutside(true);
            dialogLevel.setContentView(view);
            dialogLevel.show();
            rlLevelContainer.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (dialogLevel != null && activity != null && !activity.isFinishing()) {
                        dialogLevel.dismiss();
                    }
                }
            }, 2000);
        }

    }


    /**
     * 签到成功的弹窗
     *
     * @param activity
     * @param value    加的积分
     */
    public void showSignpDialog(final Activity activity, String value, String title, String content) {
        this.activity = activity;
        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_sign, null);
        FrameLayout flSignContainer = (FrameLayout) view.findViewById(R.id.fl_signcontainer);
        TextView tv_value = (TextView) view.findViewById(R.id.tv_value);
        ImageView ivIcon = (ImageView) view.findViewById(R.id.iv_icon);
        TextView tvContent = (TextView) view.findViewById(R.id.content);

        try {
            ivIcon.setImageResource(R.drawable.task_icon_sign_tip);

        } catch (Exception e) {
            e.printStackTrace();
        }


        if (TextUtils.isEmpty(value)) {
            tv_value.setVisibility(View.GONE);

        } else {
            tv_value.setText("+" + value);
        }
        String prom = "";
        if (!TextUtils.isEmpty(title)) {
            prom += title;
        }
        if (!TextUtils.isEmpty(content)) {
            prom += "\n" + content;
        }

        if (TextUtils.isEmpty(prom)) {
            tvContent.setVisibility(View.GONE);
        } else {
            tvContent.setVisibility(View.VISIBLE);
            tvContent.setText(prom);
        }

        if (!activity.isFinishing()) {
            //
            final Dialog dialogSign = new Dialog(activity, R.style.CustomDialogStyle);
            dialogSign.requestWindowFeature(Window.FEATURE_ACTION_MODE_OVERLAY);
            dialogSign.getWindow().setGravity(Gravity.CENTER);
            dialogSign.setCanceledOnTouchOutside(true);
            dialogSign.setContentView(view);

            dialogSign.show();
            flSignContainer.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (dialogSign != null && activity != null && !activity.isFinishing()) {
                        dialogSign.dismiss();
                    }
                }
            }, 3000);
        }
    }

    /**
     * 持续签到成功的弹窗
     *
     * @param activity
     * @param value    加的积分
     * @param handler  点击领取后的操作
     */
    public void showAlwaysSignpDialog(final Activity activity, final String value, final Handler handler) {
        this.activity = activity;
        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_always_sigin, null);
        TextView tv1 = (TextView) view.findViewById(android.R.id.text1);
        if (UserInforManager.getInstance().isLogin()) {
            tv1.setText(UserInforManager.getInstance().getUserInfor().getTask().getRankName() +
                    ",  您已连续签到" + value + "天");
        } else {
            tv1.setText("您已连续签到" + value + "天");
        }

        if (!activity.isFinishing()) {
            //
            final Dialog dialogSign = new Dialog(activity, R.style.CustomDialogStyle);
            view.findViewById(android.R.id.button2).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (dialogSign != null && activity != null && !activity.isFinishing()) {
                        dialogSign.dismiss();
                    }
                }
            });
            view.findViewById(android.R.id.button1).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (dialogSign != null && activity != null && !activity.isFinishing()) {
                        dialogSign.dismiss();
                    }
                    if (handler != null) {
                        handler.sendEmptyMessage(0);
                    }
                    StatService.onEvent(activity, "050", "连续签到领取奖励", 1);//统计事件的点击次数
                }
            });
            dialogSign.requestWindowFeature(Window.FEATURE_ACTION_MODE_OVERLAY);
//            dialogSign.getWindow().setGravity(Gravity.CENTER);
            dialogSign.setCanceledOnTouchOutside(false);

            dialogSign.setContentView(view);

            dialogSign.show();
        }
    }


    /*********
     * 权限的dialog
     **********************************************************/


    public void showPermissionDialog(Activity activity, boolean first, String[] pers) {
        this.activity = activity;
        this.pers = pers;
        View permissionView = LayoutInflater.from(activity).inflate(R.layout.dialog_permission, null);

        TextView desc = (TextView) permissionView.findViewById(R.id.desc);
        TextView cancle = (TextView) permissionView.findViewById(R.id.per_cancel);
        TextView next = (TextView) permissionView.findViewById(R.id.per_next);
        TextView setting = (TextView) permissionView.findViewById(R.id.per_setting);

        if (desc == null ||
                cancle == null ||
                next == null ||
                setting == null) {
            return;
        }

        if (pers != null && pers.length > 0) {
            StringBuilder str = new StringBuilder();
            for (int i = 0; i < pers.length; i++) {
                if (pers[i].equals(Manifest.permission.READ_PHONE_STATE)) {
                    str.append("通过电话权限确定本机设备ID,以保证为您个性化推荐资讯内容\n");
                }
                if (pers[i].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    str.append("通过获取存储空间权限,以保证为您能正常下载和更新应用,保存图片\n");
                }

                if (pers[i].equals(Manifest.permission.READ_EXTERNAL_STORAGE)) {
                    str.append("通过获取存储空间权限,以保证您可以正常的查看相册\n");
                }

                if (pers[i].equals(Manifest.permission.CAMERA)) {
                    str.append("通过获取相机功能权限,确保您能更换头像\n");
                }
            }
            desc.setText(str);
        }


        if (first) {
            cancle.setVisibility(View.GONE);
            setting.setVisibility(View.GONE);
            next.setVisibility(View.VISIBLE);
            next.setOnClickListener(this);
        } else {
            cancle.setVisibility(View.VISIBLE);
            setting.setVisibility(View.VISIBLE);
            next.setVisibility(View.GONE);
            cancle.setOnClickListener(this);
            setting.setOnClickListener(this);

        }


        if (!activity.isFinishing()) {
            //
            permissionDialog = new Dialog(activity, R.style.CustomDialogStyle);
            permissionDialog.requestWindowFeature(Window.FEATURE_ACTION_MODE_OVERLAY);
            permissionDialog.getWindow().setGravity(Gravity.CENTER);
            permissionDialog.setCanceledOnTouchOutside(false);
            permissionDialog.setContentView(permissionView);

            if (!permissionDialog.isShowing()) {
                permissionDialog.show();

            }
        }


    }


}
